function plot_phy_dump_pss_corr(filename)
%filename -- our PHY dump file: *.dat
% filename = 'logs\maxim-res17874\scatter-master-fix-channels-srn30-RES17874\scatter_iq_dump_node_id_30_20171007_181814.dat';
% filename = 'logs\maxim-res17874\scatter-master-fix-channels-srn30-RES17874\scatter_iq_dump_node_id_30_20171007_182139.dat'; % central 0MHz, pass BW 8.4MHz stop BW 10MHz
% filename = 'logs\maxim-res17862\scatter-master-fix-channels-srn122-RES17862\scatter_iq_dump_node_id_122_20171007_170207.dat';
% filename = 'logs\maxim-res17862\scatter-master-fix-channels-srn122-RES17862\scatter_iq_dump_node_id_122_20171007_170700.dat';
% filename = 'logs\maxim-res17711\scatter-scrimmage5-srn17-RES17711\scatter_iq_dump_node_id_17_20171006_204640.dat';
% filename = 'logs\maxim-res17838\scatter-master-fix-channels-srn6-RES17838\scatter_iq_dump_node_id_6_20171007_121309.dat';

sampling_rate = 23.04e6;
num_points = 8192;
nfft = 512;

filename_mat = [filename(1:(end-4)), '.mat'];

target_ratio = 4*3;
% delete(filename_mat);
if isempty(dir(filename_mat))
    fir_coef_pss1M = [0.000476237535760886,0.000273735178818135,0.000343872532493016,0.000418789482643665,0.000496472593597790,0.000574656601189872,0.000650547719144128,0.000721218106692920,0.000783317896598193,0.000833520512824605,0.000868181694560656,0.000883921811767116,0.000877321091514879,0.000845536637080681,0.000786038093271290,0.000697122453394594,0.000577616618343996,0.000427361428431318,0.000246990232796742,3.84106839079256e-05,-0.000195473802919563,-0.000450459158083994,-0.000721502507779463,-0.00100239901733778,-0.00128614525929148,-0.00156454773915048,-0.00182904304539905,-0.00207053706973769,-0.00228032774822259,-0.00244914865124812,-0.00256838065656829,-0.00263004834006145,-0.00262820847858149,-0.00255650256880897,-0.00241171221770744,-0.00219224676613173,-0.00189782030183661,-0.00153168140344193,-0.00109839295882033,-0.000605792698471911,-6.35730115756280e-05,0.000515868214025532,0.00111836653665288,0.00172763566652011,0.00232608746627304,0.00289490837924335,0.00341483366668725,0.00386648637497203,0.00423108314604820,0.00449089721621947,0.00462996024444286,0.00463466213042395,0.00449437736795835,0.00420190636665702,0.00375395368909456,0.00315151969381361,0.00240030924604062,0.00151076150679154,0.000498180093107792,-0.000617476258360582,-0.00181147518731875,-0.00305501533191185,-0.00431560374406941,-0.00555752319806890,-0.00674259696856371,-0.00783122156176493,-0.00878310491481698,-0.00955801206375676,-0.0101174874595916,-0.0104248958287430,-0.0104473648627857,-0.0101561776793790,-0.00952792006997572,-0.00854525085901721,-0.00719755653883404,-0.00548158106527571,-0.00340174766491193,-0.000970497798583636,0.00179182395597120,0.00485701350733100,0.00818941968537864,0.0117463932386786,0.0154790890997360,0.0193333308489062,0.0232506141105985,0.0271692921192734,0.0310257907554535,0.0347560451030697,0.0382966828189440,0.0415865651171997,0.0445679621865867,0.0471880678828564,0.0493998631378353,0.0511633693958117,0.0524466016914405,0.0532260280164801,0.0534874430453774,0.0532260280164801,0.0524466016914405,0.0511633693958117,0.0493998631378353,0.0471880678828564,0.0445679621865867,0.0415865651171997,0.0382966828189440,0.0347560451030697,0.0310257907554535,0.0271692921192734,0.0232506141105985,0.0193333308489062,0.0154790890997360,0.0117463932386786,0.00818941968537864,0.00485701350733100,0.00179182395597120,-0.000970497798583636,-0.00340174766491193,-0.00548158106527571,-0.00719755653883404,-0.00854525085901721,-0.00952792006997572,-0.0101561776793790,-0.0104473648627857,-0.0104248958287430,-0.0101174874595916,-0.00955801206375676,-0.00878310491481698,-0.00783122156176493,-0.00674259696856371,-0.00555752319806890,-0.00431560374406941,-0.00305501533191185,-0.00181147518731875,-0.000617476258360582,0.000498180093107792,0.00151076150679154,0.00240030924604062,0.00315151969381361,0.00375395368909456,0.00420190636665702,0.00449437736795835,0.00463466213042395,0.00462996024444286,0.00449089721621947,0.00423108314604820,0.00386648637497203,0.00341483366668725,0.00289490837924335,0.00232608746627304,0.00172763566652011,0.00111836653665288,0.000515868214025532,-6.35730115756280e-05,-0.000605792698471911,-0.00109839295882033,-0.00153168140344193,-0.00189782030183661,-0.00219224676613173,-0.00241171221770744,-0.00255650256880897,-0.00262820847858149,-0.00263004834006145,-0.00256838065656829,-0.00244914865124812,-0.00228032774822259,-0.00207053706973769,-0.00182904304539905,-0.00156454773915048,-0.00128614525929148,-0.00100239901733778,-0.000721502507779463,-0.000450459158083994,-0.000195473802919563,3.84106839079256e-05,0.000246990232796742,0.000427361428431318,0.000577616618343996,0.000697122453394594,0.000786038093271290,0.000845536637080681,0.000877321091514879,0.000883921811767116,0.000868181694560656,0.000833520512824605,0.000783317896598193,0.000721218106692920,0.000650547719144128,0.000574656601189872,0.000496472593597790,0.000418789482643665,0.000343872532493016,0.000273735178818135,0.000476237535760886];
    [data, position] = read_complex(filename,57600000,'int16', 0);
    sig0 = ddc(data, sampling_rate, sampling_rate/target_ratio, fir_coef_pss1M, 7.5e6);
    sig1 = ddc(data, sampling_rate, sampling_rate/target_ratio, fir_coef_pss1M, 2.5e6);
    sig2 = ddc(data, sampling_rate, sampling_rate/target_ratio, fir_coef_pss1M, -2.5e6);
    sig3 = ddc(data, sampling_rate, sampling_rate/target_ratio, fir_coef_pss1M, -7.5e6);

    save(filename_mat, 'data', 'sig0', 'sig1', 'sig2', 'sig3');
end
tmp = load(filename_mat);
data = tmp.data;
figure; plot_spec(data, sampling_rate, num_points, 'b'); title('spectrum');
figure; spectrogram(data,kaiser(nfft,0.5),nfft/4 ,nfft ,sampling_rate ,'centered');  title('waterfall');

sig0 = tmp.sig0;
sig1 = tmp.sig1;
sig2 = tmp.sig2;
sig3 = tmp.sig3;

% [fd_pss, td_pss] = pss_gen;
tmp1 = load('td_pss_seq_cid0.mat');
td_pss_seq_cid0 = tmp1.td_pss_seq_cid0;
sig0_pss_corr_abs = abs(conv(sig0, conj(td_pss_seq_cid0(end:-1:1).')));
sig1_pss_corr_abs = abs(conv(sig1, conj(td_pss_seq_cid0(end:-1:1).')));
sig2_pss_corr_abs = abs(conv(sig2, conj(td_pss_seq_cid0(end:-1:1).')));
sig3_pss_corr_abs = abs(conv(sig3, conj(td_pss_seq_cid0(end:-1:1).')));

t_tmp = 1e3.*(0:(length(sig0)-1))./(sampling_rate/target_ratio);
t_tmp_pss = 1e3.*(0:(length(sig0_pss_corr_abs)-1))./(sampling_rate/target_ratio);
figure; 
subplot(2,1,1); plot(t_tmp, abs(sig0)); xlabel('time(ms)'); ylabel('abs');grid on; title('channel 0');
subplot(2,1,2); plot(t_tmp_pss, sig0_pss_corr_abs);xlabel('ms'); ylabel('pss corr peak');grid on; title('channel 0');
figure;
subplot(2,1,1); plot(t_tmp, abs(sig1)); xlabel('time(ms)'); ylabel('abs');grid on; title('channel 1');
subplot(2,1,2); plot(t_tmp_pss, sig1_pss_corr_abs);xlabel('ms'); ylabel('pss corr peak');grid on; title('channel 1');
figure;
subplot(2,1,1); plot(t_tmp, abs(sig2)); xlabel('time(ms)'); ylabel('abs');grid on; title('channel 2');
subplot(2,1,2); plot(t_tmp_pss, sig2_pss_corr_abs);xlabel('ms'); ylabel('pss corr peak');grid on; title('channel 2');
figure;
subplot(2,1,1); plot(t_tmp, abs(sig3)); xlabel('time(ms)'); ylabel('abs');grid on; title('channel 3');
subplot(2,1,2); plot(t_tmp_pss, sig3_pss_corr_abs);xlabel('ms'); ylabel('pss corr peak');grid on; title('channel 3');

